package fudan.se.project.service.infrastructure.func.account;

import fudan.se.project.domain.User;
import fudan.se.project.domain.sysrole.SystemAuthority;
import fudan.se.project.service.infrastructure.entity.UserService;
import fudan.se.project.service.infrastructure.entity.sysrole.SystemAuthorityService;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.stereotype.Service;

@Service
public class AccountRegisterService {

    private final SystemAuthorityService systemAuthorityService;
    private final UserService userService;


    public AccountRegisterService(SystemAuthorityService systemAuthorityService, UserService userService) {
        this.systemAuthorityService = systemAuthorityService;
        this.userService = userService;
    }


    public void process(User user, String rawPassword){
        final SystemAuthority authUser = systemAuthorityService.
                getSystemAuthority(SystemAuthority.Authority.USER);
        String encodedPassword = userService.encode(rawPassword);
        user.setPassword(encodedPassword);
        user.setSystemAuthorities(new HashSet<>(Collections.singletonList(authUser)));

        userService.save(user);

    }

}
